<template>
	<view class="pat">
		<view class="pat-img">
			<block v-for="(item, index) in imageList" :key="index">
				<view style="margin-top: 20rpx;"  @longpress="longFun"
				v-for="(ele, idx) in item.url" :key="idx" @tap="lookImg(item,ele)">
					<u--image :src="ele" width="346rpx" height="346rpx" radius="10">
						<view slot="error" style="font-size: 24rpx;">
							<view style="padding-left: 14rpx;"><u-icon name="photo" color="#909193" size="28"></u-icon>
							</view>
							<view>加载失败</view>
						</view>
						<template v-slot:loading>
							<u-loading-icon color="#FF6005"></u-loading-icon>
						</template>
					</u--image>
				</view>
			</block>

		</view>
		<view class="look-box" v-if="imgUrl" @click="imgUrl=''">
			<movable-area scale-area>
				<movable-view direction="all" @scale="onScale" scale="true" scale-min="1" scale-max="4">
					<image mode="widthFix" :src="imgUrl" ></image>
				</movable-view>
			</movable-area>
			<view class="look-btn">
				<view class="other">
					<view class="name">{{items.prompt}}</view>
					<view class="time">{{items.insert_time}}</view>
				</view>
				<view class="btn">
					<view @click.stop="downloadImage"><u-icon name="download" color="#ffffff" size="28"></u-icon></view>
					<view @click.stop="shareImg"><u-icon name="share-square" color="#ffffff" size="28"></u-icon></view>
				</view>
			</view>
		</view>
		
		<view v-show="imageList.length <= 0" class="nodata">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png">
			</u-empty>
		</view>
		
	</view>
</template>

<script>
	export default {
		props: {
			imageList: {
				type: Array
			}
		},
		data() {
			return {
				imgUrl: '',
				items: ''
			}
		},
		onLoad() {},
		methods: {
			shareImg() {
				uni.showLoading({
					title: '请稍后...'
				})
				wx.downloadFile({
					url: this.imgUrl, //分享的图片的链接
					success: (res) => {
						uni.hideLoading()
						console.log('res', res)
						wx.showShareImageMenu({
							path: res.tempFilePath
						})
					}
				})

			},
			downloadImage() {
				uni.showLoading({
					title: '请稍后...'
				})
				
				
				wx.downloadFile({
					url: this.imgUrl, //分享的图片的链接
					success: (res) => {
						uni.hideLoading()
						console.log('res', res)
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: function() {
								uni.hideLoading()
								uni.showModal({
									title: '保存成功',
									content: '图片已成功保存到相册',
									showCancel: false
								});
							},
							complete(res) {
								uni.hideLoading()
								console.log(res);
							}
						});
					}
				})
				// return
				// this.preservationImg()
			},
			preservationImg() {
				let that = this
				uni.authorize({
					/* scope.writePhotosAlbum 类型是保存到相册 */
					scope: 'scope.writePhotosAlbum',
					success() {
						/* 已授权进入 */
						/* 保存图片到相册方法方法 */
						that.imgApi(that.imgUrl);
					},
					complete(res) {
						/* 判断如果没有授权就打开设置选项让用户重新授权 */
						uni.getSetting({
							success(res) {
								if (!res.authSetting['scope.writePhotosAlbum']) {
									/* 打开设置的方法 */
									that.opensit();
								}
							}
						});
					}
				});
			},
			imgApi() {
				/* 获取图片的信息 */
				uni.getImageInfo({
					src: this.imgUrl,
					success: function(image) {
						/* 保存图片到手机相册 */
						uni.saveImageToPhotosAlbum({
							filePath: image.path,
							success: function() {
								uni.hideLoading()
								uni.showModal({
									title: '保存成功',
									content: '图片已成功保存到相册',
									showCancel: false
								});
							},
							complete(res) {
								uni.hideLoading()
								console.log(res);
							}
						});
					}
				});
			},

			opensit() {
				uni.hideLoading()
				uni.showModal({
					content: '没有授权保存图片到相册,点击确定去允许授权',
					success: function(res) {
						if (res.confirm) {
							/* 打开设置的API*/
							uni.openSetting({
								success(res) {
									console.log(res.authSetting);
								}
							});
						} else if (res.cancel) {
							uni.showModal({
								cancelText: '取消',
								confirmText: '重新授权',
								content: '你点击了取消，将无法进行保存操作',
								success: function(res) {
									if (res.confirm) {
										uni.openSetting({
											success(res) {
												/* 授权成功 */
												console.log(res.authSetting);
											}
										});
									} else if (res.cancel) {
										console.log('用户不授权');
									}
								}
							});
						}
					}
				});
			},

			lookImg(item, url) {
				this.imgUrl = url
				this.items = item
			},
			longFun() {
				console.log('111')
			}
		}
	}
</script>

<style lang="scss" scoped>
	movable-view {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100%;
	}

	movable-area {
		height: 100%;
		width: 100%;
		position: fixed;
		overflow: hidden;
	}

	movable-view image {
		width: 100%;
	}

	.look-box {
		height: 100%;
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background-color: rgba(0, 0, 0, 0.8);
		display: flex;
		align-items: center;

		.look-btn {
			width: 100%;
			position: fixed;
			bottom: var(--window-bottom);
			left: 0;
			z-index: 2;
			background-color: rgba(0, 0, 0, 0.1);
			padding: 20rpx 20rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.other {
				font-size: 26rpx;
				color: #efefef;
				width: 500rpx;

				.name {
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.btn {
				width: 140rpx;
				display: flex;
				justify-content: space-between;
			}
		}

		.icon {
			position: absolute;
			top: calc(var(--window-top) + 20rpx);
			right: 10rpx;
		}

		image {
			width: 100%;
		}
	}

	.pat {
		margin: 0rpx 20rpx;

		.pat-img {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			width: 100%;
			// .img {
			// 	margin-top: 20rpx;
			// 	width: 346rpx;
			// 	border-radius: 20rpx;
			// 	// height: auto;
			// 	height: 350rpx;
			// }
		}
	}
	.nodata {
		width: 100%;
		margin-top: 100rpx;
	}
	
</style>